package com.punai.monthanalysis.bo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.multi.ListValueMap;
import cn.hutool.core.util.StrUtil;
import com.punai.common.utils.NumberUtil;
import com.punai.common.utils.bean.BeanUtils;
import com.punai.externalccb.bo.YdCbfeeiteminfoStandardBo;
import com.punai.externalccb.domain.YdCbfeeiteminfoStandard;
import com.punai.monthanalysis.domain.ProYdcbfxGcjdkhcldbfxb;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class ProYdcbfxGcjdkhcldbfxbBo extends ProYdcbfxGcjdkhcldbfxb {

    private Integer rowspan = 1;
    private Integer colspan = 1;
    private List<ProYdcbfxGcjdkhcldbfxbBo> children = new ArrayList<>();

    public Integer getRowspan() {
        return rowspan;
    }

    public void setRowspan(Integer rowspan) {
        this.rowspan = rowspan;
    }

    public Integer getColspan() {
        return colspan;
    }

    public void setColspan(Integer colspan) {
        this.colspan = colspan;
    }

    public List<ProYdcbfxGcjdkhcldbfxbBo> getChildren() {
        return children;
    }

    public void setChildren(List<ProYdcbfxGcjdkhcldbfxbBo> children) {
        this.children = children;
    }

    public void clearColRow() {
        this.setRowspan(0);
        this.setColspan(0);
    }

    public static List<ProYdcbfxGcjdkhcldbfxbBo> convertStandard(List<YdCbfeeiteminfoStandardBo> list){
        List<ProYdcbfxGcjdkhcldbfxbBo> result = new ArrayList<>();
        if (CollUtil.isEmpty(list)) {
            return null;
        }
        for (YdCbfeeiteminfoStandardBo standard : list) {
            ProYdcbfxGcjdkhcldbfxbBo gcjdkhcldbfxb = new ProYdcbfxGcjdkhcldbfxbBo();
            gcjdkhcldbfxb.setId(standard.getId());
            gcjdkhcldbfxb.setProId(standard.getProId());
            gcjdkhcldbfxb.setQc(standard.getYdDate());//期次Id
            gcjdkhcldbfxb.setItemName(standard.getCategoryName()==null?"":standard.getCategoryName());//分类名称
            gcjdkhcldbfxb.setItemDetailName(standard.getName());
            gcjdkhcldbfxb.setUnit(standard.getUnit());

            BigDecimal ysTotal = standard.getMonthCzTotal();
            gcjdkhcldbfxb.setYsQuantity(standard.getMonthAmount());
            gcjdkhcldbfxb.setYsAvgPrice(standard.getMonthPrice());
            gcjdkhcldbfxb.setYsPrice(standard.getMonthCzTotal());

            gcjdkhcldbfxb.setSjQuantity(standard.getActualCostAmout());
            gcjdkhcldbfxb.setSjAvgPrice(standard.getActualCostNoTaxPrice());
            gcjdkhcldbfxb.setSjPrice(standard.getActualCostNoTaxTotal());

            BigDecimal jce = standard.getKlDifferenceOfCzsjTotal();
            BigDecimal jcl = NumberUtil.divideNew(jce, 2, ysTotal);
            gcjdkhcldbfxb.setJcQuantity(standard.getDifferenceOfCzsjAmount());
            gcjdkhcldbfxb.setJce(standard.getKlDifferenceOfCzsjTotal());
            gcjdkhcldbfxb.setJcl(jcl);
            gcjdkhcldbfxb.setJcReason(standard.getMainReason());
            result.add(gcjdkhcldbfxb);
        }
        Map<String, List<ProYdcbfxGcjdkhcldbfxbBo>> listMap = result.stream().filter(v -> v.getItemName() != null).collect(Collectors.groupingBy(ProYdcbfxGcjdkhcldbfxbBo::getItemName, Collectors.toList()));
        return  convert(listMap);
    }
    public static List<ProYdcbfxGcjdkhcldbfxbBo> convert(Map<String, List<ProYdcbfxGcjdkhcldbfxbBo>> listMap) {
        List<ProYdcbfxGcjdkhcldbfxbBo> result = new ArrayList<>();
        if (CollUtil.isEmpty(listMap)) {
            return result;
        }
        for (Map.Entry<String, List<ProYdcbfxGcjdkhcldbfxbBo>> listEntry : listMap.entrySet()) {
            List<ProYdcbfxGcjdkhcldbfxbBo> entryValues = listEntry.getValue();
            for (int i = 0; i < entryValues.size(); i++) {
                ProYdcbfxGcjdkhcldbfxbBo bo = entryValues.get(i);
                if (i == 0){
                    bo.setRowspan(entryValues.size());
                }else {
                    bo.setItemName(null);
                    bo.clearColRow();
                }
                result.add(bo);
            }
        }
        return result;
    }

    public static List<ProYdcbfxGcjdkhcldbfxbBo> convert(List<ProYdcbfxGcjdkhcldbfxb> inList) {
        List<ProYdcbfxGcjdkhcldbfxbBo> result = new ArrayList<>();
        if (CollUtil.isEmpty(inList)) {
            return result;
        }

        ListValueMap<String,ProYdcbfxGcjdkhcldbfxb> levelMap = new ListValueMap<>();
        for (ProYdcbfxGcjdkhcldbfxb gcjdkhcldbfxb : inList) {
            levelMap.putValue(StrUtil.blankToDefault(gcjdkhcldbfxb.getPid(),"0"),gcjdkhcldbfxb);
        }
        List<ProYdcbfxGcjdkhcldbfxb> roots = levelMap.get("0");
        roots.sort(Comparator.comparing(ProYdcbfxGcjdkhcldbfxb::getXh));


        ProYdcbfxGcjdkhcldbfxbBo hjBo = new ProYdcbfxGcjdkhcldbfxbBo();
        // 只拆解两层
        for (ProYdcbfxGcjdkhcldbfxb root : roots) {
            String id = root.getId();
            ProYdcbfxGcjdkhcldbfxbBo convert = convert(root);
            List<ProYdcbfxGcjdkhcldbfxb> children = levelMap.get(id);
            if (CollUtil.isNotEmpty(children)) {
                convert.setRowspan(children.size() + 1);

                result.add(convert);
                children.sort(Comparator.comparing(ProYdcbfxGcjdkhcldbfxb::getXh));

                for (ProYdcbfxGcjdkhcldbfxb child : children) {
                    ProYdcbfxGcjdkhcldbfxbBo childrenBo = convert(child);
                    childrenBo.clearColRow();
                    result.add(childrenBo);
                }
            } else {
                result.add(convert);
            }
        }
        hjBo.setItemName("合计");
        hjBo.setYsPrice(roots.stream().map(ProYdcbfxGcjdkhcldbfxb::getYsPrice).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add));
        hjBo.setSjPrice(roots.stream().map(ProYdcbfxGcjdkhcldbfxb::getSjPrice).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add));
        hjBo.setJce(roots.stream().map(ProYdcbfxGcjdkhcldbfxb::getJce).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add));
        hjBo.setJcl(NumberUtil.divide(hjBo.getJce(),3,hjBo.getYsPrice()));
        result.add(hjBo);
        return result;
    }

    public static ProYdcbfxGcjdkhcldbfxbBo convert(ProYdcbfxGcjdkhcldbfxb in) {
        ProYdcbfxGcjdkhcldbfxbBo out = new ProYdcbfxGcjdkhcldbfxbBo();
        BeanUtils.copyBeanProp(out,in);
        return out;
    }


}
